package com.wangsh.projDemo.demo.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsh.projDemo.common.pojo.ApiResponse;
import com.wangsh.projDemo.common.pojo.ApiResponseEnum;
import com.wangsh.projDemo.common.pojo.JSONEnum;
import com.wangsh.projDemo.common.util.ConstatFinalUtil;
import com.wangsh.projDemo.common.util.PageInfoUtil;
import com.wangsh.projDemo.demo.dao.IADemoDynastyDao;
import com.wangsh.projDemo.demo.dao.IADemoKingDao;
import com.wangsh.projDemo.demo.pojo.ADemoDynasty;
import com.wangsh.projDemo.demo.pojo.ADemoKing;
import com.wangsh.projDemo.demo.service.IDemoService;

/**
 * Service实现类(示例代码)
 * 
 * @author Wangsh
 */
@Service("demoService")
public class DemoServiceImplay implements IDemoService
{
	@Resource
	private IADemoDynastyDao demoDynastyDAO;
	@Resource
	private IADemoKingDao demoKingDAO;
	
	@Override
	public ApiResponse<Object> saveOneDynastyService(ADemoDynasty dynasty)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		try
		{
			dynasty.setAge(dynasty.getEdYear() - dynasty.getStYear());
			
			int res = this.demoDynastyDAO.save(dynasty);
			if (res > 0)
			{
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put(JSONEnum.EFFECT.getName(), res);
				dataMap.put(JSONEnum.ID.getName(), dynasty.getId());
				dataMap.put(ApiResponseEnum.NAME_ONE.getName(), dynasty);
				apiResponse.setDataOne(dataMap);
				
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			} else
			{
				apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
			}
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("保存一条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneDynastyService(ADemoDynasty dynasty)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		try
		{
			dynasty.setAge(dynasty.getEdYear() - dynasty.getStYear());
			
			int res = this.demoDynastyDAO.update(dynasty);
			if (res > 0)
			{
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put(JSONEnum.EFFECT.getName(), res);
				dataMap.put(JSONEnum.ID.getName(), dynasty.getId());
				dataMap.put(ApiResponseEnum.NAME_ONE.getName(), dynasty);
				apiResponse.setDataOne(dataMap);
				
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			} else
			{
				apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
			}
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("更新一条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneDynastyService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		try
		{
			int res = this.demoDynastyDAO.delete(condMap);
			if (res > 0)
			{
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put(JSONEnum.EFFECT.getName(), res);
				apiResponse.setDataOne(dataMap);
				
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			} else
			{
				apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
			}
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("删除一条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<ADemoDynasty> findCondListDynastyService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		ApiResponse<ADemoDynasty> apiResponse = new ApiResponse<ADemoDynasty>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ADemoDynasty> demoDynastyList = Collections.EMPTY_LIST;
		
		Map<String, List<ADemoDynasty>> dataList = new HashMap<String, List<ADemoDynasty>>();
		try
		{
			
			/* 对关键字进行模糊匹配 */
			if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
			{
				condMap.put("keyword", "%" + condMap.get("keyword") + "%") ; 
			}
			
			/* 设置分页相关信息 */
			if(pageInfoUtil != null)
			{
				/* 设置分页信息 */
				Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
				demoDynastyList = this.demoDynastyDAO.findList(condMap) ; 
				pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
				/* 向接口中放分页信息 */
				apiResponse.setPageInfoUtil(pageInfoUtil);
			}else
			{
				demoDynastyList = this.demoDynastyDAO.findList(condMap) ; 
			}
			/* 将结果放到JSON中 */
			dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoDynastyList);
			
			apiResponse.setDataListJava(demoDynastyList);
			apiResponse.setDataList(dataList);
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("查询多条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse; 
	}

	@Override
	public ApiResponse<ADemoDynasty> findOneDynastyService(Map<String, Object> condMap)
	{
		ApiResponse<ADemoDynasty> apiResponse = new ApiResponse<ADemoDynasty>();
			
		Map<String, ADemoDynasty> data = new HashMap<String, ADemoDynasty>();
		try
		{
			ADemoDynasty dynasty = this.demoDynastyDAO.findOne(condMap);
			data.put(ApiResponseEnum.NAME_ONE.getName(), dynasty);
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			/* 将结果放到JSON中 */
			apiResponse.setDataOne(data);
			apiResponse.setDataOneJava(dynasty);
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("查询一条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse ; 
	}
	
	@Override
	public ApiResponse<Object> saveOneKingService(ADemoKing king)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		try
		{
			int res = this.demoKingDAO.save(king);
			if (res > 0)
			{
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put(JSONEnum.EFFECT.getName(), res);
				dataMap.put(JSONEnum.ID.getName(), king.getId());
				dataMap.put(ApiResponseEnum.NAME_ONE.getName(), king);
				apiResponse.setDataOne(dataMap);
				
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			} else
			{
				apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
			}
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("保存一条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneKingService(ADemoKing king)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		try
		{
			int res = this.demoKingDAO.update(king);
			if (res > 0)
			{
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put(JSONEnum.EFFECT.getName(), res);
				dataMap.put(JSONEnum.ID.getName(), king.getId());
				dataMap.put(ApiResponseEnum.NAME_ONE.getName(), king);
				apiResponse.setDataOne(dataMap);
				
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			} else
			{
				apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
			}
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("更新一条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneKingService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		try
		{
			int res = this.demoKingDAO.delete(condMap);
			if (res > 0)
			{
				Map<String, Object> dataMap = new HashMap<String, Object>();
				dataMap.put(JSONEnum.EFFECT.getName(), res);
				apiResponse.setDataOne(dataMap);
				
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			} else
			{
				apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
			}
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("删除一条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<ADemoKing> findCondListKingService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		ApiResponse<ADemoKing> apiResponse = new ApiResponse<ADemoKing>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<ADemoKing> demoKingList = Collections.EMPTY_LIST;
		
		Map<String, List<ADemoKing>> dataList = new HashMap<String, List<ADemoKing>>();
		try
		{
			
			/* 对关键字进行模糊匹配 */
			if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
			{
				condMap.put("keyword", "%" + condMap.get("keyword") + "%") ; 
			}
			
			/* 设置分页相关信息 */
			if(pageInfoUtil != null)
			{
				/* 设置分页信息 */
				Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
				demoKingList = this.demoKingDAO.findList(condMap) ; 
				pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
				/* 向接口中放分页信息 */
				apiResponse.setPageInfoUtil(pageInfoUtil);
			}else
			{
				demoKingList = this.demoKingDAO.findList(condMap) ; 
			}
			/* 将结果放到JSON中 */
			dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoKingList);
			
			apiResponse.setDataListJava(demoKingList);
			apiResponse.setDataList(dataList);
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("查询多条记录失败了",e);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse; 
	}

	@Override
	public ApiResponse<ADemoKing> findOneKingService(Map<String, Object> condMap)
	{
		ApiResponse<ADemoKing> apiResponse = new ApiResponse<ADemoKing>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		
		Map<String, ADemoKing> data = new HashMap<String, ADemoKing>();
		try
		{
			ADemoKing king = this.demoKingDAO.findOne(condMap);
			data.put(ApiResponseEnum.NAME_ONE.getName(), king);
			
			/* 将结果放到JSON中 */
			apiResponse.setDataOne(data);
			apiResponse.setDataOneJava(king);
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("查询一条记录失败了",e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			/* 报错信息的占位符 */
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + "") , infoMap);
		}
		return apiResponse ; 
	}
}
